package com.tianqu.tms.www.car.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.CarStyle;
import com.tianqu.tms.entity.CommercialVehicle;
import com.tianqu.tms.entity.Vendor;
import com.tianqu.tms.www.car.actionform.CarCreationInputActionForm;
import com.tianqu.tms.www.car.viewhelper.CarCreationInputViewHelper;

public class CarCreationFindCarController extends TmsController{

	private Logger logger = Logger
	.getLogger(CarCreationFindCarController.class);
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();
		try {
			controller.begin();
			CarCreationInputActionForm inputForm = (CarCreationInputActionForm)uc.getInputForm();
			Car car = Car.getInstance();
			String vin = (String)uc.getAttribute("vin");
			CarCreationInputViewHelper viewHelper = new CarCreationInputViewHelper();
			CarCreationInputViewHelper view = (CarCreationInputViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
			if(view != null){
				viewHelper = view;
			}
			viewHelper.setFindVinFlag(0);//为了保证每次都是0值
			if(vin != null && vin != ""){
				car = car.findCarBaseInfoByVin(vin);
				if(car != null){
					if(car.getVin() != null && !"".equals(car.getVin())){
						setViewhelper(viewHelper,car);
					}
				}
				
			}else{
				car = car.findCarBaseInfoByVin(inputForm.getVin());
				if(car != null){
					if(car.getVin() != null && !"".equals(car.getVin())){
						setViewhelperFromEntity(viewHelper,car);
					}
				}else{
					viewHelper.setFindVinFlag(2);
				}
				
			}
			CommercialVehicle commercialVehicle = CommercialVehicle.getInstance();
			// 车辆品牌
			Vendor vendor = Vendor.getInstance();
			if(!StringUtil.isNullOrBlank(viewHelper.getVendorId())){
				if("V".equals(viewHelper.getVendorId().substring(0,1))){
					viewHelper.setFromType(2);
					viewHelper.setVendorList(commercialVehicle.findAllVendorInfo());
				}else if("S".equals(viewHelper.getVendorId().substring(0,1))){
					viewHelper.setFromType(1);
					viewHelper
					.setVendorList(vendor.findAllVendorInfo());
				}
			}
			String serial = (String)uc.getAttribute("serial");
			viewHelper.setSerial(serial);
			uc.setAttribute(TmsConstants.VIEWHELP, viewHelper);
			controller.commit();
			
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

		logger.debug(this.getClass().getName() + "#execute was completed.");
	}

	/**
	 * 从Entity 给viewHelper 赋值
	 * 
	 * @param viewHelper
	 * @param car
	 * @throws TmsDaoException
	 */
	private void setViewhelperFromEntity(CarCreationInputViewHelper viewHelper,
			Car car) throws TmsDaoException {
	
		CarStyle carStyle = CarStyle.getInstance();
		viewHelper.setBrandId(car.getBrandId());
		viewHelper.setCardType(Integer.toString(car.getCardType()));
		// //去掉车牌号中的'
		// String licensePlate = car.getLicensePlate().replace("'", "");
		viewHelper.setId(car.getcId());
		viewHelper.setLicensePlate(car.getLicensePlate());
		viewHelper.setCarOwner(car.getCarOwner());
		viewHelper.setCarOwnerIdCardNo(car.getCarOwnerIdCardNo());
		viewHelper.setCarType(Integer.toString(car.getCarType()));
		viewHelper.setCarUse(Integer.toString(car.getCarUse()));
		viewHelper.setColor(Integer.toString( car.getColor()));
		viewHelper.setCountryStyle(Integer.toString( car.getCountryStyle()));
		if (car.getDisplacement() == 0) {
			viewHelper.setDisplacement("");
		} else {
			viewHelper.setDisplacement(Integer.toString(car.getDisplacement()));
		}

		viewHelper.setEngine(car.getEngine());
		viewHelper.setFuelType(Integer.toString(car.getFuelType()));
		viewHelper.setGearbox(Integer.toString(car.getGearbox()));
		viewHelper.setJqx(car.getJqx());
		if (car.getLoadquality() == 0) {
			viewHelper.setLoadquality("");
		} else {
			viewHelper.setLoadquality(Integer.toString(car.getLoadquality()));
		}

		viewHelper.setCarStyleId(car.getCarStyleId());
		viewHelper.setCarName(carStyle.getCarStyleRealNameById(car
				.getCarStyleId()));
		
		
		if (car.getMileage() == 0) {
			viewHelper.setMileage("");
		} else {
			viewHelper.setMileage(StringUtil.addDH(Integer.toString(car.getMileage())));
		}

		viewHelper.setModelCode(car.getModelCode());
		viewHelper.setOwnerAddress(car.getOwnerAddress());
		viewHelper.setOwnerCardNo(car.getOwnerCardNo());
		viewHelper.setOwnerOrgNo(car.getOwnerOrgNo());
		viewHelper.setOwnerPhone(car.getOwnerPhone());
		if (car.getPassenger() == 0) {
			viewHelper.setPassenger("");
		} else {
			viewHelper.setPassenger(Integer.toString(car.getPassenger()));
		}

		viewHelper.setRegDate(car.getRegDate());
		viewHelper.setRegisterCardNo(car.getRegisterCardNo());
		viewHelper.setSendCardDate(car.getSendCardDate());
		viewHelper.setSerial(car.getSerial());
		viewHelper.setVendorCode(car.getVendorCode());
		viewHelper.setVin(car.getVin());
		viewHelper.setYearCarefulPeriod(car.getYearCarefulPeriod());
		viewHelper.setVendorId(car.getVendorId());
		viewHelper.setCarStyle(car.getCarStyleId());	
		if(viewHelper.getOwnerTypeFlag() == 2){
			viewHelper.setOwnerType(String.valueOf(car.getOwnerType()));
		}
		
	}
	
	/**
	 * @param viewHelper
	 * @param car
	 * @throws TmsDaoException
	 */
	private void setViewhelper(CarCreationInputViewHelper viewHelper,
			Car car) throws TmsDaoException {
	
		CarStyle carStyle = CarStyle.getInstance();
		viewHelper.setBrandId(car.getBrandId());
		// //去掉车牌号中的'
		// String licensePlate = car.getLicensePlate().replace("'", "");
		viewHelper.setId(car.getcId());
		viewHelper.setCarOwner(car.getCarOwner());
		viewHelper.setCarOwnerIdCardNo(car.getCarOwnerIdCardNo());
		viewHelper.setCarType(Integer.toString(car.getCarType()));
		viewHelper.setCarUse(Integer.toString(car.getCarUse()));
		viewHelper.setColor(Integer.toString( car.getColor()));

		viewHelper.setCountryStyle(Integer.toString( car.getCountryStyle()));
		if (car.getDisplacement() == 0) {
			viewHelper.setDisplacement("");
		} else {
			viewHelper.setDisplacement(Integer.toString(car.getDisplacement()));
		}

		viewHelper.setEngine(car.getEngine());
		viewHelper.setFuelType(Integer.toString(car.getFuelType()));
		viewHelper.setGearbox(Integer.toString(car.getGearbox()));
		viewHelper.setJqx(car.getJqx());
		if (car.getLoadquality() == 0) {
			viewHelper.setLoadquality("");
		} else {
			viewHelper.setLoadquality(Integer.toString(car.getLoadquality()));
		}

		viewHelper.setCarStyleId(car.getCarStyleId());
		viewHelper.setCarName(carStyle.getCarStyleRealNameById(car
				.getCarStyleId()));
		
		
		if (car.getMileage() == 0) {
			viewHelper.setMileage("");
		} else {
			viewHelper.setMileage(StringUtil.addDH(Integer.toString(car.getMileage())));
		}

		viewHelper.setModelCode(car.getModelCode());
		viewHelper.setOwnerAddress(car.getOwnerAddress());
		viewHelper.setOwnerCardNo(car.getOwnerCardNo());
		viewHelper.setOwnerOrgNo(car.getOwnerOrgNo());
		viewHelper.setOwnerPhone(car.getOwnerPhone());

		if (car.getPassenger() == 0) {
			viewHelper.setPassenger("");
		} else {
			viewHelper.setPassenger(Integer.toString(car.getPassenger()));
		}

		viewHelper.setRegDate(car.getRegDate());
		viewHelper.setRegisterCardNo(car.getRegisterCardNo());
		viewHelper.setSendCardDate(car.getSendCardDate());
		viewHelper.setVendorCode(car.getVendorCode());
		viewHelper.setVin(car.getVin());
		viewHelper.setYearCarefulPeriod(car.getYearCarefulPeriod());
		viewHelper.setVendorId(car.getVendorId());
		viewHelper.setCarStyle(car.getCarStyleId());	
		if(viewHelper.getOwnerTypeFlag() == 2){
			viewHelper.setOwnerType(String.valueOf(car.getOwnerType()));
		}
	}
}
